package com.stenio.fancat.controller.user;

import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.user.UserKinsfolkForm;
import com.stenio.fancat.form.user.UserRelationshipForm;
import com.stenio.fancat.service.user.UserRelationshipService;
import com.stenio.fancat.vo.BabyRelationshipVO;
import com.stenio.fancat.vo.UserRelationshipVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("userRelationship")
public class UserRelationshipController {

    @Autowired
    private UserRelationshipService userRelationshipService;

    /**
     * 邀请亲人
     * @param userKinsfolkForm
     * @return
     */
    @PostMapping("/inviteKinsfolk")
    public Result<Integer> inviteKinsfolk(@RequestBody UserKinsfolkForm userKinsfolkForm){
        Result<Integer> result = new Result<>();
        try {
            Integer status = userRelationshipService.inviteKinsfolk(userKinsfolkForm);
            result.setData(status);
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 被邀请页面的API
     * @param userKinsfolkForm
     * @return
     */
    @PostMapping("/invitedPage")
    public Result<BabyRelationshipVO> invitedPage(@RequestBody UserKinsfolkForm userKinsfolkForm){
        Result<BabyRelationshipVO> result = new Result<>();
        try {
            result.setData(userRelationshipService.invitedPage(userKinsfolkForm.getUserId(),userKinsfolkForm.getBabyId()));
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 已邀请的亲人
     * @param userKinsfolkForm
     * @return
     */
    @PostMapping("/inviteUserList")
    public Result<Map<String,Object>> inviteUserList(@RequestBody UserKinsfolkForm userKinsfolkForm){
        Result<Map<String,Object>> result = new Result<>();
        try {
            result.setData(userRelationshipService.inviteUserList(userKinsfolkForm.getUserId(),userKinsfolkForm.getBabyId()));
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 所有的亲人列表
     * @param userKinsfolkForm
     * @return
     */
    @PostMapping("/allKinsfolk")
    public Result<List<UserRelationshipVO>> allKinsfolk(@RequestBody UserKinsfolkForm userKinsfolkForm){
        Result<List<UserRelationshipVO>> result = new Result<>();
        try {
            result.setData(userRelationshipService.allKinsfolk(userKinsfolkForm.getUserId(),userKinsfolkForm.getBabyId()));
        } catch (FanCatException e){
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 领取糖果币（candy）  如果领完变成已领取
     * @param userKinsfolkForm
     * @return
     */
    @PostMapping("/getUserCandy")
    public Result<UserRelationshipVO> getUserCandy(@RequestBody UserKinsfolkForm userKinsfolkForm){
        Result<UserRelationshipVO> result = new Result<>();
        try {
            result.setData(userRelationshipService.getUserCandy(userKinsfolkForm.getUserId(),userKinsfolkForm.getBabyId(),userKinsfolkForm.getInviteUserId()));
        } catch (FanCatException e) {
            result.setMsg(e.getMsg());
            result.setErrorCode(e.getErrorCode());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setFlag(Result.FLAG.success);
        return result;
    }

}
